/* Copyright (c) 2021-2025 Qualcomm Technologies International, Ltd. */
/* %%version */

#include "csr_synergy.h"

#ifndef EXCLUDE_GATT_XPAN_CLIENT_MODULE

#include "gatt_xpan_client.h"
#include "gatt_xpan_client_private.h"
#include "gatt_xpan_client_debug.h"
#include "gatt_xpan_client_common.h"

static void xpanClientSendTerminateCfm(GXPANC *xpan_client)
{
    bool res = FALSE;
    AppTask app_task = xpan_client->app_task;

    MAKE_XPAN_CLIENT_MESSAGE(GattXpanClientTerminateCfm);

    message->clntHndl = xpan_client->srvcElem->service_handle;

    message->startHandle = xpan_client->clientData.startHandle;
    message->endHandle = xpan_client->clientData.endHandle;

    message->handles = xpan_client->handles;

    /*
     * The XPAN client Instance Data does not store any dynamically allocated memory, so
     * no memory needs to be freed before freeing the Instance Data.
     */

    res = ServiceHandleFreeInstanceData(xpan_client->srvcElem->service_handle);

    if (res)
    {
        message->status = GATT_XPAN_CLIENT_STATUS_SUCCESS;
    }
    else
    {
        message->status = GATT_XPAN_CLIENT_STATUS_FAILED;
    }

    XpanClientMessageSend(app_task, GATT_XPAN_CLIENT_TERMINATE_CFM, message);
}

/****************************************************************************/
void GattXpanClientTerminateReq(ServiceHandle clntHndl)
{
    GXPANC *xpan_client = ServiceHandleGetInstanceData(clntHndl);

    if (xpan_client)
    {
        /* Unregister with the GATT Manager and verify the result */
        CsrBtGattUnregisterReqSend(xpan_client->srvcElem->gattId);
        xpanClientSendTerminateCfm(xpan_client);
    }
    else
    {
        GATT_XPAN_CLIENT_PANIC("GattXpanClientTerminateReq: No XPAN client instance found\n");
    }
}

#endif /* EXCLUDE_GATT_XPAN_CLIENT_MODULE */